package com.framework.controller.system;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.framework.annotation.SystemLog;
import com.framework.controller.index.BaseController;
import com.framework.entity.IndustryFormMap;
import com.framework.entity.ResFormMap;
import com.framework.mapper.IndustryMapper;
import com.framework.plugin.PageView;
import com.framework.util.Common;


@Controller
@RequestMapping("/industry/")
public class IndustryController extends BaseController {
	List<ResFormMap> res = null;
	@Inject
	private IndustryMapper industryMapper;

	// 获取界面UI
	@RequestMapping(value = "list")
	public String listUI(Model model) throws Exception {
		res = findByRes();
		model.addAttribute("res", getButtomM(res));
		return Common.BACKGROUND_PATH + "/system/industry/list";
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage(String draw, String length, String start) throws Exception {
		IndustryFormMap industryFormMap = getFormMap(IndustryFormMap.class);
		industryFormMap.put("$orderby", getorder());
		industryFormMap.put("$search", getsearch());
		String order="order by `granddescription` desc";
		industryFormMap.put("$orderby", order);
		industryFormMap = toFormMap(industryFormMap, draw, length, start);
		pageView.setData(IndustryFormMap.mapper().findIndustryPage(industryFormMap));
		return pageView;
	}

	@ResponseBody
	@RequestMapping("getButton")
	public String getButton() throws Exception {
		return getButtomB(res);
	}

	// 显示添加内容的页面
	@RequestMapping("addUI")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/system/industry/add";
	}

	// 添加活动详细内容
	@ResponseBody
	@RequestMapping("addEntity")
	@SystemLog(module = "系统管理", methods = "城市管理-新增城市") // 凡需要处理业务逻辑的.都需要记录操作日志
	@Transactional(readOnly = false) // 需要事务操作必须加入此注解
	public String addEntity(Model model) throws Exception {
		IndustryFormMap industryFormMap = getFormMap(IndustryFormMap.class);
		// 插入一条新活动信息
		industryFormMap.save();
		return "success";
	}

	// 删除活动数据
	@ResponseBody
	@RequestMapping("deleteEntity")
	@SystemLog(module = "系统管理", methods = "城市管理-删除城市") // 凡需要处理业务逻辑的.都需要记录操作日志
	@Transactional(readOnly = false) // 需要事务操作必须加入此注解
	public String deleteEntity() throws Exception {
		IndustryFormMap formMap = new IndustryFormMap();
		String[] ids = getParaValues("ids");
		if (ids.length > 0) {
			for (int i = 0; i < ids.length; i++) {
				formMap.deleteById(ids[i]);
//				ActdropdownFormMap downFormMap = new ActdropdownFormMap();
//				downFormMap.put("cityid", ids[i]);
//				// 删除活动的同时去删除对应下拉框的数据
//				downFormMap.deleteByNames();
			}
		}
		return "success";
	}

	// 显示修改活动页面
	@RequestMapping("editUI")
	public String editUI(Model model) {
		String id = getPara("id");
		if (Common.isNotEmpty(id)) {
			model.addAttribute("industryFormMap", new IndustryFormMap().findById(id));
		}
		return Common.BACKGROUND_PATH + "/system/industry/edit";
	}

	// 修改活动
	@ResponseBody
	@RequestMapping("editEntity")
	@SystemLog(module = "系统管理", methods = "城市管理-修改城市") // 凡需要处理业务逻辑的.都需要记录操作日志
	@Transactional(readOnly = false) // 需要事务操作必须加入此注解
	public String editEntity(Model model) throws Exception {
		IndustryFormMap industryFormMap = getFormMap(IndustryFormMap.class);
		if (Common.isNotEmpty(industryFormMap.get("id").toString())) {
			industryFormMap.update();
		}
		return "success";
	}
	/////////// ----------- 只管上面这个 这里就得到数据了 ----------///////////////

	// 验证方法
	@ResponseBody
	@RequestMapping("isExist")
	public boolean isExist(String name) {
		IndustryFormMap industryFormMap = getFormMap(IndustryFormMap.class);
		List<IndustryFormMap> list = industryFormMap.findByNames();
		if (list.size() == 0) {
			return true;
		} else {
			return false;
		}
	}

	// 修改时的验证方法
	@ResponseBody
	@RequestMapping("editisExist")
	public boolean editisExist() {
		IndustryFormMap industryFormMap = getFormMap(IndustryFormMap.class);
		// 先根据名称去找，没有就返回true
		// 有的话就判断是不是这id的记录，如果是的话也返回true
		// 否则返回false
		List<IndustryFormMap> list = industryFormMap.findByAttribute("id", industryFormMap.get("id"));
		if (list.size() == 0) {
			return true;
		} else {
			if (list.size() >= 0) {
				List<IndustryFormMap> list2 = industryFormMap.findByNames();
				if (list2.size() > 0) {
					return true;
				} else {
					return false;
				}
			}
			return false;
		}
	}

}
